function [data2] = loadDataFilesCtsTime_pairAdoptions(dataId)

if dataId == 3
	timeAggLevel = 'day';
	geoAggLevel =  'CantonId';
	
	% Dataset for covariates
	datasetName = 'CantonId_day';
	datasetFile = sprintf('../../data/data_mat/covariates/covariates_%s.mat', datasetName);

	% Dataset for pair adoptions in continuous time
	inputTransactionFile = '../../data/data_mat/platformData/pairAdoptionsData_CantonId_ctsTime.mat';
end

if strcmp(timeAggLevel, 'week'); tt_subset_idxes = 1:68;  end;
if strcmp(timeAggLevel, 'day');  tt_subset_idxes = 1:476; end;

locationAggregationsFolder = sprintf('../../data/data_mat/timeLocationAggregations/location/%s', geoAggLevel);
timeAggregationsFolder     = sprintf('../../data/data_mat/timeLocationAggregations/time/%s',     timeAggLevel);

%%%%%%%%%% LOAD TRANSACTIONS DATA (pair adoptions) %%%%%%%%%%
disp(sprintf('Loading %s...', inputTransactionFile));
load(inputTransactionFile, 'timestampsData');
pairAdoptionData = timestampsData; clear timestampsData;
	% pairAdoptionData:	object
	% 	.geoLabels: 			cell(K,1) of strings
	% 	.timestamps:			NumAdoptions x 1
	% 	.kk_consumer:				NumAdoptions x 1 (integer)
	% 	.kk_provider:				NumAdoptions x 1 (integer)

geoLabels = pairAdoptionData.geoLabels;
K = length(geoLabels);

% Streamline/recode pairAdoptionData
pairAdoptionData = rmfield(pairAdoptionData, 'geoLabels');
pairAdoptionData.kk_consumer = double(pairAdoptionData.kk_consumer);
pairAdoptionData.kk_provider = double(pairAdoptionData.kk_provider);


%%%%%%%%%% GET INITIAL CONDITIONS (before tt = 0) %%%%%%%%%%
% Make initLaggedConsumersOrig and initLaggedConsumersDest
idxes = find(pairAdoptionData.timestamps < 0);
initLaggedConsumersOrig = accumarray(pairAdoptionData.kk_consumer(idxes), pairAdoptionData.kk_consumer(idxes), [K 1], @length); % K_consumer x 1
initLaggedConsumersDest = accumarray(pairAdoptionData.kk_provider(idxes),  pairAdoptionData.kk_provider(idxes),  [K 1], @length); % K_provider x 1


%%%%%%%%%% COMBINE ORDERED EVENTS TOGETHER %%%%%%%%%%
% Make a matrix that contains all events (pair adoption, day change)
% Here I assume periodicity at day level!
periodChanges = tt_subset_idxes';

NumPairAdoptions = length(pairAdoptionData.timestamps);
NumPeriodChanges   = length(tt_subset_idxes);

pairAdoptionData = [pairAdoptionData.timestamps pairAdoptionData.kk_consumer pairAdoptionData.kk_provider 1*ones(NumPairAdoptions, 1)]; % NumPairAdoptions x 4
periodChangeData   = [periodChanges                -1*ones(NumPeriodChanges,1)   -1*ones(NumPeriodChanges,1) 4*ones(NumPeriodChanges,1)];    % NumPeriods x 1

allEventsData = [pairAdoptionData; periodChangeData]; % NumEvents x 4

% Clean up
clear pairAdoptionData periodChangeData;
clear NumPairAdoptions NumPeriodChanges;


%%%%%%%%%% CONSTRUCT SPELLS FROM ORDERED EVENTS %%%%%%%%%%
% Remove events before tt=0
event_dates = allEventsData(:,1);
allEventsData = allEventsData(event_dates >= 0,:); % I am assuming periodicity at day level here!
clear event_dates;

% Remove events after last day considered
event_dates = allEventsData(:,1);
allEventsData = allEventsData(event_dates <= max(tt_subset_idxes),:); % I am assuming periodicity at day level here!
clear event_dates;

% Sort allEventsData by increasing timestamp
[~,sortidxes] = sort(allEventsData(:,1));
allEventsData = allEventsData(sortidxes,:);
clear sortidxes;

% Compute Y
NumSpells = size(allEventsData,1);
Y.pairAdoptions = sparse(NumSpells, K*K); % NumSpells x (K_consumer * K_provider)  <=> NumSpells x (K1*K2)
Y.pairAdoptionsOrig = sparse(NumSpells, K); % NumSpells x K_consumer  <=> NumSpells x K1
Y.pairAdoptionsDest = sparse(NumSpells, K); % NumSpells x K_provider  <=> NumSpells x K2

for tt = 1:NumSpells
	if allEventsData(tt,4) == 1 % pair adoption
		kk_consumer = allEventsData(tt,2);
		kk_provider  = allEventsData(tt,3);
		myidx = sub2ind([K,K],kk_consumer,kk_provider);
		Y.pairAdoptions(tt, myidx) = 1;
		Y.pairAdoptionsOrig(tt, kk_consumer) = 1;
		Y.pairAdoptionsDest(tt, kk_provider)  = 1;
	end
end
clear kk_consumer kk_provider myidx;

% Make laggedConsumersOrig, laggedConsumersDest
laggedConsumersOrig = initLaggedConsumersOrig' + cumsum(Y.pairAdoptionsOrig, 1) - Y.pairAdoptionsOrig; % NumSpells x K_consumers
laggedConsumersDest = initLaggedConsumersDest' + cumsum(Y.pairAdoptionsDest, 1) - Y.pairAdoptionsDest; % NumSpells x K_providers

% Compute spell durations
event_dates = allEventsData(:,1);         % NumSpells x 1
spellStarts = [0; event_dates(1:end-1)];  % NumSpells x 1
spellEnds   = event_dates;                % NumSpells x 1
spellDurations = spellEnds - spellStarts; % NumSpells x 1
clear event_dates;

% Make mapping from spell to day (at start of spell) --> I am assuming periodicity at day level here!
spell2period = make_integers(floor(spellStarts) +  1); % NumSpells x 1 (gives values between 1 and NumPeriods)


%%%% Store everything into an object ctsTimeData
ctsTimeData.NewPairs    = Y.pairAdoptions; % NumSpells x (K1*K2) - sparse
ctsTimeData.laggedConsumersOrig = laggedConsumersOrig; % NumSpells x K1  - sparse
ctsTimeData.laggedConsumersDest = laggedConsumersDest; % NumSpells x K2  - sparse
ctsTimeData.spellDurations    = spellDurations;    % NumSpells x 1
ctsTimeData.spell2period      = spell2period;      % NumSpells x 1 (integer between 1 and NumPeriods)


%%%%%%%%%% LOAD PERIODIC DATA of covariates %%%%%%%%%
configPeriodicData.datasetFile                = datasetFile;
configPeriodicData.timeAggLevel               = timeAggLevel;
configPeriodicData.geoAggLevel                = geoAggLevel;
configPeriodicData.surfaceVar_Zname           = 'Area';
configPeriodicData.laggedAdoptionEffects      = {{}}; % (ignored any way...)
configPeriodicData.locationAggregationsFolder = locationAggregationsFolder;
configPeriodicData.timeAggregationsFolder     = timeAggregationsFolder;


data_tmp = loadPeriodicData(configPeriodicData, tt_subset_idxes);
periodicData      = data_tmp.periodicData;
periodLabels      = data_tmp.periodLabels;
population        = data_tmp.population;
surfaces          = data_tmp.surfaces;
incomingCommuters = data_tmp.incomingCommuters;
touristBeds       = data_tmp.touristBeds;
surfaceName       = data_tmp.surfaceName;

assert(isequal(data_tmp.geoLabels, geoLabels));

clear data_tmp;


%%%%%% Store everything into data2
data2.ctsTimeData  = ctsTimeData;
data2.periodicData = periodicData;

data2.geoLabels    = geoLabels;
data2.periodLabels  = periodLabels;

data2.population   = population;
data2.surfaces     = surfaces;
data2.incomingCommuters = incomingCommuters;
data2.touristBeds       = touristBeds;
data2.surfaceName       = surfaceName;

end
